import type { IAddress } from '@/types/address';
import type { IResponse } from '../types/index';

const data: IAddress[] = [];

export default [
  {
    url: '/mock/address',
    method: 'get',
    response: (): IResponse<IAddress[]> => {
      return {
        code: 200,
        msg: 'ok',
        data,
      };
    },
  },
  {
    url: '/mock/address/add',
    method: 'post',
    response: ({ body }: { body: IAddress }): IResponse<IAddress> => {
      body.id = Date.now();
      data.push(body);
      return {
        code: 200,
        msg: 'ok',
        data: body,
      };
    },
  },
  {
    url: '/mock/address/edit',
    method: 'put',
    response: ({ body }: { body: IAddress }): IResponse<IAddress> => {
      const index = data.findIndex((item) => item.id == body.id);
      if (index != -1) {
        data[index] = body;
        return {
          code: 200,
          msg: 'ok',
          data: body,
        };
      }
      return {
        code: 0,
        msg: '修改失败',
        data: body,
      };
    },
  },
  {
    url: '/mock/address/delete',
    method: 'delete',
    response: ({ body }: { body: { id: number } }): IResponse => {
      const index = data.findIndex((item) => item.id == body.id);
      if (index != -1) {
        data.splice(index, 1);
        return {
          code: 200,
          msg: 'ok',
          data: null,
        };
      }
      return {
        code: 0,
        msg: '删除失败',
        data: null,
      };
    },
  },
];
